from pinpong.extension.unihiker import *
from pinpong.board import Board,Pin
from unihiker import GUI
import requests
import time
import sys
import os

surprised = 0
exit_now = 0
u_gui=GUI()
timenow1 = u_gui.draw_text(text="",x=60,y=280,font_size=20, color="#0000FF")
def time_now():
    timenow = time.strftime("%H:%M:%S")
    timenow1.config(text=timenow)

def button_click1():
    global XuHao
    global city
    global ChengShiMing
    if (XuHao <= 1):
        XuHao = 5
    else:
        XuHao = (XuHao - 1)
    ChengShiMing = (city[XuHao])
    city_x.config(text=ChengShiMing)
def button_click2():
    global XuHao
    global city
    global ChengShiMing
    if (XuHao >= 5):
        XuHao = 1
    else:
        XuHao = (XuHao + 1)
    ChengShiMing = (city[XuHao])
    city_x.config(text=ChengShiMing)
def restart():
    u_gui.clear()
    def restart_program():
        python = sys.executable
        os.execl(python, python, * sys.argv)
    if __name__ == "__main__":
        restart_program()
def on_buttonb_click_callback():
    global surprised
    surprised = surprised + 1
    print(surprised)
    if surprised == 5:
        u_gui.clear()
        u_gui.draw_image(image="Easter_egg.jpeg",x=0,y=0)
        u_gui.draw_text(text="曾家乐，我喜欢你", x=10, y=30, font_size=20, color="#000000")
        emoji=u_gui.draw_emoji(emoji="Wink",x=0,y=0,duration=0.2)
    if surprised == 10:
        restart()

Board().begin()
u_gui.on_b_click(on_buttonb_click_callback)
city = {1: "上海",2:"深圳",3:"化州",4:"东莞",5:"雷州"}
TianQiTuPian = {"晴": "晴.png","霾":"霾.png","阴":"阴天.png","多云":"多云.png"}
XuHao = 1
t1=u_gui.draw_text(text="选择城市",x=80,y=10,font_size=15, color="#0000FF")
u_gui.add_button(text="重启",x=0,y=280,w=50,h=40,onclick=restart)
kuang=u_gui.draw_rect(x=80,y=50,w=80,h=30,width=2,color="#000000")
shang=u_gui.add_button(text="↑",x=40,y=50,w=30,h=30,onclick=button_click1)
xia=u_gui.add_button(text="↓",x=170,y=50,w=30,h=30,onclick=button_click2)
city_x=u_gui.draw_text(text=(city[XuHao]),x=100,y=50,font_size=15, color="#0000FF")
ChengShiMing = (city[XuHao])
url = (str("https://www.yiketianqi.com/free/day?appid=62445587&appsecret=jMyj6HoU&unescape=1&city=") + str(ChengShiMing))
response = requests.get(url)
data = response.json()
t_c=u_gui.draw_text(text=ChengShiMing,x=35,y=120,font_size=20, color="#000000")
t_t=u_gui.draw_text(text=(data["date"]),x=25,y=160,font_size=10, color="#993300")
t_w=u_gui.draw_text(text=(str("天气:") + str((data["wea"]))),x=80,y=200,font_size=15, color="#0000FF")
t_tem=u_gui.draw_text(text=(str("温度:") + str((str((data["tem"])) + str("°C")))),x=75,y=240,font_size=15, color="#0000FF")
img=u_gui.draw_image(image=(TianQiTuPian[(data["wea"])]),x=180,y=100)
img.config(origin="top")
img.config(h=80)

while True:
    time_now()
    if (button_a.is_pressed()==True):
        #exit_now = 0 #Bug方案一
        url = (str("https://www.yiketianqi.com/free/day?appid=62445587&appsecret=jMyj6HoU&unescape=1&city=") + str(ChengShiMing))
        response = requests.get(url)
        data = response.json()
        t_c.config(text=ChengShiMing)
        t_t.config(text=(data["date"]))
        t_w.config(text=(str("天气:") + str((data["wea"]))))
        t_tem.config(text=(str("温度:") + str((str((data["tem"])) + str("°C")))))
        img.config(image=(TianQiTuPian[(data["wea"])]))